package com.pink.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pink.entity.dto.UserDto;
import com.pink.entity.dto.VideoInfoDto;
import com.pink.entity.query.VideoInfoQuery;
import com.pink.entity.vo.response.VideoNameVo;
import com.pink.exception.BusinessException;
import com.pink.mappers.VideoInfoMapper;
import com.pink.service.VideoInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VideoInfoServiceImpl extends ServiceImpl<VideoInfoMapper, VideoInfoDto> implements VideoInfoService {

    @Override
    public String addVideoInfo(VideoInfoDto videoInfoDto, UserDto user) {
        if(videoInfoDto == null){
            throw new BusinessException("视频信息不能为空");
        }
        videoInfoDto.setUserId(Long.valueOf(user.getUserId()));
        //根据时间计算是周一~周日
        videoInfoDto.setUpdateDate(getChineseWeekday(videoInfoDto.getReleaseDate()));
        this.save(videoInfoDto);
        return "视频信息添加成功";
    }

    @Override
    public VideoInfoQuery findVideoInfoQueryList(int pageNo, int pageSize, String title) {
        List<VideoInfoDto> videoInfoDtoList = List.of();
        if(title == ""){
            videoInfoDtoList = this.list();
        }
        if(title != ""){
            videoInfoDtoList = this.lambdaQuery().like(VideoInfoDto::getTitle,title).list();
        }
        VideoInfoQuery videoQuery = new VideoInfoQuery();
        videoQuery.setPage(pageNo);
        videoQuery.setPageNo(pageNo);
        videoQuery.setPageSize(pageSize);
        videoQuery.setPageTotal((videoInfoDtoList.size() + pageSize - 1) / pageSize);
        videoQuery.setTotalCount(videoInfoDtoList.size());
        if(videoInfoDtoList.size() > pageSize){
            videoInfoDtoList = videoInfoDtoList.subList((pageNo - 1) * pageSize, pageNo * pageSize);
        }
        videoQuery.setList(videoInfoDtoList);
        return videoQuery;
    }

    @Override
    public String updateVideoInfo(VideoInfoDto videoInfo, UserDto user) {
        if(user == null){
            throw new BusinessException("用户信息不能为空");
        }
        if(videoInfo == null){
            throw new BusinessException("视频信息不能为空");
        }
        VideoInfoDto videoInfoDto = this.lambdaQuery().eq(VideoInfoDto::getVideoId,videoInfo.getVideoId()).one();
        if(videoInfoDto == null){
            throw new BusinessException("视频不存在");
        }
        videoInfo.setUserId(Long.valueOf(user.getUserId()));
        //根据更新时间计算是周一~周日
        videoInfo.setUpdateDate(getChineseWeekday(videoInfoDto.getReleaseDate()));
        this.update(videoInfo,new LambdaUpdateWrapper<VideoInfoDto>().eq(VideoInfoDto::getVideoId,videoInfo.getVideoId()));
        return "视频信息更新成功";
    }

    @Override
    public String deleteVideoInfo(Integer videoId) {
        if(videoId == null){
            throw new BusinessException("视频ID不能为空");
        }
        VideoInfoDto videoInfoDto = this.lambdaQuery().eq(VideoInfoDto::getVideoId,videoId).one();
        if(videoInfoDto == null){
            throw new BusinessException("视频不存在");
        }
        this.remove(new QueryWrapper<VideoInfoDto>().eq("video_id", videoId));
        return "视频信息删除成功";
    }

    @Override
    public List<VideoNameVo> findVideoNameList() {
        List<VideoNameVo> videoNameVos = new ArrayList<>();
        List<VideoInfoDto> videoInfoDto = this.list();
        if (videoInfoDto != null) {
            for (VideoInfoDto dto : videoInfoDto) {
                VideoNameVo vo = new VideoNameVo();
                //复制属性
                BeanUtils.copyProperties(dto, vo);
                videoNameVos.add(vo);
            }
        }
        return videoNameVos;
    }

    //获取轮播图列表
    @Override
    public List<VideoInfoDto> getBannerList() {
        //按更新时间降序排列 获取前五条数据
        return this.lambdaQuery().orderByDesc(VideoInfoDto::getUpdatedAt).last("limit 5").list();
    }

    @Override
    public List<VideoInfoDto> getPopularList() {
        //获取viewCount前4的视频
        return this.lambdaQuery().orderByDesc(VideoInfoDto::getViewCount).last("limit 4").list();
    }

    @Override
    public List<VideoInfoDto> getRecentList(String dateTime) {
        //获取2个月以内的视频列表
        // 按更新时间降序排列
        // 按updateDate 查询40条
        return this.lambdaQuery().eq(VideoInfoDto::getUpdateDate,dateTime)
                .orderByDesc(VideoInfoDto::getUpdatedAt)
                .last("limit 40").list();

    }

    @Override
    public VideoInfoDto getVideoInfo(Integer videoId) {
        return this.lambdaQuery().eq(VideoInfoDto::getVideoId,videoId).one();
    }

    private String getChineseWeekday(Date dateSet) {
        // 定义中文星期数组（索引与ISO-8601标准对齐）
        String[] CHINESE_WEEKDAYS = {
                "",         // 占位
                "周一", "周二", "周三",
                "周四", "周五", "周六", "周日"
        };
        // 格式化日期为ISO格式字符串
        String formattedDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(dateSet);
        LocalDate date = LocalDate.parse(formattedDate);

        // 获取星期枚举值
        DayOfWeek dayOfWeek = date.getDayOfWeek();

        // 返回对应中文星期
        return CHINESE_WEEKDAYS[dayOfWeek.getValue()];
    }
}